/* This .do file provides the code that produces Figures 3 and 4 in the main text and Figures A7-A10 of the Online Supplementary Materials of:

Paglayan, Agustina S. 2021. "From Rebellion to Indoctrination: The Violent Rise of Primary Education Systems." American Political Science Review. 

Before executing the code, download the following datasets in your selected path/ directory:
Chile_Escuelas y Alumnos_pcias. y total pais 1859-1879.csv
Chile_Fiscal Capacity statistics, 1810-1995.dta
Civil War and Primary Enrollment_firstwar_APSR.dta

The code below was generated with Stata version 16.

*/

clear all
set more off, perm
global path "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Papers\Submission\APSR\APSR Replication\"  	// PATH TO THE DATASETS
cd "${path}"
global graphs "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Papers\Submission\APSR\APSR Figures and Tables\"  	// WHERE TO SAVE GRAPHS

* FIGURE 3: PUBLIC PRIMARY SCHOOLS AND ENROLLMENT IN CHILE, 1859-1878
*____________________________________________________________________

clear
insheet using "${path}Chile_Escuelas y Alumnos_pcias. y total pais 1859-1879.csv", names case comma
gen Stateid=_n
list prov Stateid
drop if Stateid==18 | Stateid==19 | Stateid==20
reshape long esc stud escfisc studfisc escmun studmun totpopn, i(prov) j(year)
replace esc=escfisc if esc==.  
label var esc "Central government primary schools"
replace stud=studfisc if stud==.
label var esc "Students in central government primary schools"
label var totpopn "Provincial population"
bysort prov: ipolate totpopn year, gen(itotpopn) epolate
gen escpc=.
replace escpc=(esc/itotpopn)*10000 if esc!=. & itotpopn!=.
gen studpc=.
replace studpc=(stud/itotpopn)*100 if stud!=. & itotpopn!=.
gen enrollrate=. 
replace enrollrate=(stud/(itotpopn*0.20))*100 if stud!=. & itotpopn!=.
sort prov year

* PANEL A: All provinces

preserve
collapse (sum) esc escpc stud studpc itotpopn (mean) enrollrate , by(year)
graph twoway (line escpc year, lcolor(black) lpattern(solid) lwidth(thick)) (line enrollrate year, lcolor(black) lpattern(dash) lwidth(thick) yaxis(2)) if year<=1878, ///
	ytitle("Central government primary schools" "per 10,000 inhabitants") ///
	ytitle("Student enrollment rate (% pop. ages 5-14)", axis(2)) ///
	xtitle("Year") ///
	xlin(1863, lcolor(gs8) lpattern(dash)) ///
	text(75 1863 "Implementation of" "General Law of Primary" "Education begins", color(gs8) size(small) orientation(horizontal)) ///
	legend(order(1 2) label(1 "Schools/10,000 inhab. (left)") label(2 "Student Enrollment (right)") span) ///
	scheme(s1manual) scale(0.9) name(Fig3_PanelA, replace)
graph export "${graphs}Fig3,PanelA.pdf", replace
restore

* PANEL B: Atacama Vs. Rest of Chile

preserve
gen Atacama=(prov=="Atacama")
tab prov Atacama
collapse (mean) esc escpc stud studpc enrollrate, by(year Atacama)
graph twoway (line escpc year if Atacama==1, lcolor(red) lpattern(solid) lwidth(thick)) /// 
	(line escpc year if Atacama==0, lcolor(gs11) lpattern(solid) lwidth(thick)) /// 
	(line enrollrate year if Atacama==1, lcolor(red) lpattern(dash) lwidth(thick) yaxis(2)) ///
	(line enrollrate year if Atacama==0, lcolor(gs11) lpattern(dash) lwidth(thick) yaxis(2)) if year<=1878, ///
	ytitle("Student enrollment rate (% pop. ages 5-14)", axis(2)) ///
	ytitle("Central government primary schools" "per 10,000 inhabitants") ///
	xtitle("Year") ///
	xlin(1863, lcolor(gs8) lpattern(dash)) ///
	text(5.85 1863 "Implementation of" "General Law of Primary" "Education begins", color(gs8) size(small) orientation(horizontal)) ///
	legend(order(1 2 3 4) label(1 "Schools, Atacama (left)") label(2 "Schools, Rest of Chile (left)") label(3 "Student Enrollment, Atacama (right)") label(4 "Student Enrollment, Rest of Chile (right)") size(small) span) ///
	scheme(s1manual) scale(0.9) name(Fig3_PanelB, replace)
graph export "${graphs}Fig3,PanelB.pdf", replace
restore

* FIGURE 4: PUBLIC PRIMARY SCHOOLS, % CHANGE BY PARTICIPATION IN THE 1859 CHILEAN CIVIL WAR
*__________________________________________________________________________________________

clear
insheet using "${path}Chile_Escuelas y Alumnos_pcias. y total pais 1859-1879.csv", names case comma
gen Stateid=_n
list prov Stateid
drop if Stateid==18 | Stateid==19 | Stateid==20
reshape long esc stud escfisc studfisc escmun studmun totpopn, i(prov) j(year)
replace esc=escfisc if esc==.  
label var esc "Central government primary schools"
bysort prov: ipolate totpopn year, gen(itotpopn) epolate
gen escpc=.
replace escpc=(esc/itotpopn)*10000 if esc!=. & itotpopn!=.
sort prov year

preserve
gen civilwarthreat=0
replace civilwarthreat=1 if prov=="Talca" | prov=="Colchagua" | prov=="Aconcagua" | prov=="Coquimbo"
replace civilwarthreat=2 if prov=="Atacama" 
collapse (mean) esc escpc , by(year civilwarthreat)
graph twoway 	(line escpc year if civilwarthreat==2, lcolor(red) lpattern(solid) lwidth(thick)) /// 
	(line escpc year if civilwarthreat==1, lcolor(orange) lpattern(dash) lwidth(thick)) /// 
	(line escpc year if civilwarthreat==0, lcolor(gs14) lpattern(solid) lwidth(thick)) /// 
	if year<=1878, ///
	ytitle("Central government primary schools" "per 10,000 inhabitants") ///
	xtitle("") ///
	xlin(1863, lcolor(gs8) lpattern(dash)) ///
	text(5.85 1863 "Implementation of" "General Law of Primary" "Education begins", color(gs8) size(small) orientation(horizontal)) ///
	legend(order(1 2 3) label(1 "Atacama") label(2 "Easily defeated rebel provinces") label(3 "Non-rebel provinces") ) ///
	scheme(s1manual) scale(0.9) name(Fig4, replace)
graph export "${graphs}Fig4.pdf", replace
restore

* FIGURE A7: PRIMARY SCHOOL ENROLLMENT RATE IN CHILE, 1853-1878
*____________________________________________________________________

use "${path}Civil War and Primary Enrollment_firstwar_APSR.dta", clear

sort year
graph twoway (line primratio year, lcolor(gs8) lwidth(thick)) if year>=1853 & year<=1878 & WDICountryVWCode =="CHL", /// 
xlin(1859, lpattern(dash) lcolor(red) lwidth(thin)) ///
xlin(1860 1863, lpattern(dash) lcolor(gs8) lwidth(thin)) ///
text(16 1859 "Civil War", placement(w) orientation(vertical) color(black)) ///
text(16 1860 "Ley General de Instruccion Primaria", placement(w) orientation(vertical) color(gs8)) ///
text(16 1863 "Reglamento de Instruccion Primaria", placement(w) orientation(vertical) color(gs8)) ///
legend(off) ///
ytitle("Primary School Enrollment Rate" "(as a % of the school-age population)")  /// 
xtitle("Year") scheme(s1manual) scale(0.7) ///
xlabel(1850(5)1880) name(FigA7, replace)

* FIGURE A8. PUBLIC PRIMARY SCHOOLS AND STUDENT ENROLLMENT IN ATACAMA COMPARED TO A SYNTHETIC CONTROL, 1859-1878
*_______________________________________________________________________________________________________________

clear
insheet using "${path}Chile_Escuelas y Alumnos_pcias. y total pais 1859-1879.csv", names case comma
gen Stateid=_n
list prov Stateid
drop if Stateid==18 | Stateid==19 | Stateid==20
reshape long esc stud escfisc studfisc escmun studmun totpopn, i(prov) j(year)
replace esc=escfisc if esc==.
replace stud=studfisc if stud==.
bysort prov: ipolate totpopn year, gen(itotpopn) epolate
gen escpc=.
replace escpc=(esc/itotpopn)*10000 if esc!=. & itotpopn!=.
gen studpc=.
replace studpc=(stud/itotpopn)*100 if stud!=. & itotpopn!=.
gen enrollrate=. 
replace enrollrate=(stud/(itotpopn*0.20))*100 if stud!=. & itotpopn!=.
sort prov year

tsset Stateid year
save "${path}Chile_Escuelas y Alumnos_Atacama y total pais 1859-1879 for Synth.dta", replace

synth escpc escpc, trunit(17) trperiod(1863) counit(1 2 3 4  6 7 8) figure mspeperiod(1859(1)1862) keep(synth_escpc.dta, replace)  	
synth enrollrate enrollrate, trunit(17) trperiod(1863) counit(1 2 3 4 6 7 8) figure mspeperiod(1859(1)1862) keep(synth_enrollrate.dta, replace)	

use "${path}synth_escpc.dta", clear
graph twoway (line _Y_treated _time, lcolor(red) lpattern(solid) lwidth(thick)) ///
	(line _Y_synthetic _time, lcolor(red) lpattern(dash) lwidth(medium)) if _time<=1878, ///
	xlabel(1860(5)1880) ///
	ytitle("Schools per 10,000 inhabitants") ///
	xtitle(Year) ///
	xlin(1863, lpattern(dash) lcolor(gs8) lwidth(thin)) ///
	text(5.75 1863 "Implementation of" "General Law of Primary" "Education begins", color(gs8) size(small) orientation(horizontal)) ///
	scheme(s1manual) scale(0.75) legend(order(1 2) label(1 "Atacama") label(2 "Synthetic control")) name(FigA7_PanelA, replace)
	
use "${path}synth_enrollrate.dta", clear
graph twoway (line _Y_treated _time, lcolor(red) lpattern(solid) lwidth(thick)) ///
	(line _Y_synthetic _time, lcolor(red) lpattern(dash) lwidth(medium)) if _time<=1878, ///
	xlabel(1860(5)1880) ///
	ytitle("Primary School Enrollment Rate" "(as a % of the school-age population)")  /// 
	xtitle(Year) ///
	xlin(1863, lpattern(dash) lcolor(gs8) lwidth(thin)) ///
	text(15 1863 "Implementation of" "General Law of Primary" "Education begins", color(gs8) size(small) orientation(horizontal)) ///
	scheme(s1manual) scale(0.75) legend(order(1 2) label(1 "Atacama") label(2 "Synthetic control")) name(FigA7_PanelB, replace)

* FIGURE A9: PUBLIC PRIMARY SCHOOLS AND ENROLLMENT IN ATACAMA AND COQUIMBO, 1859-1878
*____________________________________________________________________________________

clear
insheet using "${path}Chile_Escuelas y Alumnos_pcias. y total pais 1859-1879.csv", names case comma
gen Stateid=_n
list prov Stateid
drop if Stateid==18 | Stateid==19 | Stateid==20
reshape long esc stud escfisc studfisc escmun studmun totpopn, i(prov) j(year)
replace esc=escfisc if esc==.
replace stud=studfisc if stud==.
bysort prov: ipolate totpopn year, gen(itotpopn) epolate
gen escpc=.
replace escpc=(esc/itotpopn)*10000 if esc!=. & itotpopn!=.
gen studpc=.
replace studpc=(stud/itotpopn)*100 if stud!=. & itotpopn!=.
gen enrollrate=. 
replace enrollrate=(stud/(itotpopn*0.20))*100 if stud!=. & itotpopn!=.
sort prov year

preserve
graph twoway (line escpc year if prov=="Atacama", lcolor(red) lpattern(solid) lwidth(thick)) /// 
	(line escpc year if prov=="Coquimbo", lcolor(gs11) lpattern(solid) lwidth(thick)) /// 
	(line enrollrate year if prov=="Atacama", lcolor(red) lpattern(dash) lwidth(thick) yaxis(2)) ///
	(line enrollrate year if prov=="Coquimbo", lcolor(gs11) lpattern(dash) lwidth(thick) yaxis(2)) if year<=1878, ///
	ytitle("Student enrollment rate (% pop. ages 5-14)", axis(2)) ///
	ytitle("Schools per 10,000 inhabitants") ///
	xtitle("Year") ///
	legend(order(1 2 3 4) label(1 "Schools, Atacama (left)") label(2 "Schools, Coquimbo (left)") label(3 "Students, Atacama (right)") label(4 "Students, Coquimbo (right)") ) ///
	scheme(s1manual) scale(0.9) name(FigA9, replace)
restore

* FIGURE A10: CENTRALLY-CONTROLLED PUBLIC PRIMARY SCHOOLS IN ATACAMA AND FISCAL REVENUE IN CHILE, 1859-1878
*__________________________________________________________________________________________________________

use "C:\Users\pagla\Dropbox\Stanford\Dissertation\Data\Chile_Escuelas y Alumnos_Atacama y total pais 1853-1879.dta", clear
keep if prov=="Atacama"
sort year
merge year using "C:\Users\pagla\Dropbox\Stanford\Dissertation\Civil War, State Consolidation, and the Spread of Mass Education\Raw Data\Chile_Fiscal Capacity statistics, 1810-1995.dta"

sort year
graph twoway (line Fiscal_Income_pctGDP year if year>=1858 & year<1878, lcolor(gs8) lpattern(solid) lwidth(thick)) /// 
(line centralgovtschools2pc year if year>=1859 & year<1878, yaxis(2) lcolor(red) lpattern(solid) lwidth(thick)) , ///
xlin(1859, lpattern(dash) lcolor(black) lwidth(thin)) ///
xlin(1860 1863, lpattern(dash) lcolor(gs8) lwidth(thin)) ///
text(6 1859 "Civil War", placement(w) orientation(vertical) color(black) size(small)) ///
text(6 1860 "Ley General de Instruccion Primaria", placement(w) orientation(vertical) color(black) size(small)) ///
text(6 1863 "Reglamento de Instruccion Primaria", placement(w) orientation(vertical) color(black) size(small)) ///
legend(order(1 2) label(1 "Fiscal Revenue (left)") label(2 "Primary Schools in Atacama (right)") span) ///
ytitle("Fiscal Revenue (% of GDP)")  ytitle("Schools per 10,000 inhabitants", axis(2)) /// 
xtitle("Year") scheme(s1manual) scale(0.9) ///
xlabel(1860(5)1880) name(FigA10, replace)

